/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import DTO.CFields;
import DTO.CHistory;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author quangphuc
 */
public class CHistoryDAO extends IData<CHistory>{

    @Override
    public CHistory add(CHistory item) {
        try {
            ps.setString(1, item.getChuc_nang());
            ps.setString(2, item.getMota());
            ps.setString(3, item.getNguoiTao());
            ps.setTimestamp(4, item.getNgayTao());
            ps.addBatch();
        } catch (SQLException ex) {
            Logger.getLogger(CHistoryDAO.class.getName()).log(Level.SEVERE, null, ex);
            throw new UnsupportedOperationException(ex.getMessage()); //To change body of generated methods, choose Tools | Templates.
        }
        return item;
    }

    @Override
    public void saveBatch() {
        
    }

    @Override
    public int addrows(List<CHistory> list, SubConfig config) {
        String mysql = "insert into " + CFields.tb_lich_su + "(" 
                + CFields.chuc_nang + "," + CFields.mo_ta + ","
                +CFields.nguoi_tao + "," + CFields.ngay_tao
                + ") values(?,?,?,?)";
        int rs = 0;
        try {
            ps = config.connec.prepareStatement(mysql);
            for(CHistory item :list){
                this.add(item);
            }
            int []n = ps.executeBatch();
            rs = n.length;
        } catch (SQLException ex) {
            Logger.getLogger(CHistoryDAO.class.getName()).log(Level.SEVERE, null, ex);
            throw new UnsupportedOperationException(ex.getMessage()); //To change body of generated methods, choose Tools | Templates.
        }
       return rs;
    }

    @Override
    public CHistory Find(CHistory item, List<CHistory> list) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    protected Object OnSubmit(CHistory item, int t, SubConfig config) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public Object insertOnSubmit(CHistory item, SubConfig config) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public Object updateOnSubmint(CHistory item, SubConfig config) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public Object deleteOnSubmint(CHistory item, SubConfig config) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    protected CHistory getItem(ResultSet rs) {
        CHistory item = new CHistory();
        try {
            item.setChuc_nang(rs.getString(CFields.chuc_nang));
            item.setId(rs.getLong(CFields.id));
            item.setMota(rs.getString(CFields.mo_ta));
            item.setNguoiTao(rs.getString(CFields.nguoi_tao));
            item.setNgayTao(rs.getTimestamp(CFields.ngay_tao));
        } catch (SQLException ex) {
            Logger.getLogger(CHistoryDAO.class.getName()).log(Level.SEVERE, null, ex);
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
        return item;
    }

    @Override
    public List<CHistory> getAll(SubConfig config) {
        String mysql = "select * from " + CFields.tb_lich_su + " " + this.sWhere + " order by id desc";
        return this.getAll(mysql, config);
    }

    @Override
    protected void add(CHistory item, int t) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

   
    
}
